import { getOrderList } from "../../../api.js"
Page({
  data: {
    tab: [{
      type: "allOrders",
      text: "全部订单"
    }, {
      type: "ordersToPay",
      text: "待付款",
    }, {
      type: "ordersToDeliver",
      text: "待发货"
    }, {
      type: "ordersToReceive",
      text: "待收货"
    }, {
      type: "ordersDone",
      text: "已完成"
    }]
  },
  onLoad(options) {
    this.init(options)
  },
  init(options) {
    let currentType = options.type || "allOrders"
    this.freshOrderList(currentType)
  },
  // 切换订单列表
  toggleOrderList(e) {
    let currentType = e.target.dataset.type
    this.setData({
      currentList: this.orderList[currentType],
      currentType
    })
  },
  // 订单操作后刷新
  afterOrderInvoke(e) {
    let currentType = this.data.currentType
    this.freshOrderList(currentType)
  },

  // 刷新订单列表
  freshOrderList(currentType) {
    let token = getApp().globalData.Store.token
    getOrderList(token).then(res => {
      if (res.data.code === 666) {
        this.orderList = res.data.data
        this.setData({
          currentList: this.orderList[currentType],
          currentType
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  onShow() {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  }
})